package path;

import geom.Vector;
import geom.VectorSpace;

import java.util.ArrayList;
import java.util.Iterator;



import processing.core.PApplet;

public class DrawLineCluster implements PathDrawer{
	ArrayList lines;
	float width;
	
	
	public DrawLineCluster(int paths, float width) {
		this.width = width;
		lines = new ArrayList();
		for(int i = 0; i < paths; i++)
			lines.add(new ArrayList());
	}

	public void drawPath(Path path, PApplet pa) {
		pa.noFill();
		pa.stroke(230, 50, 100, 150);
		Iterator li = lines.iterator();
		while(li.hasNext()){
			pa.beginShape(/*PApplet.LINE_STRIP*/);
			Iterator pi = ((ArrayList)li.next()).iterator();
			while(pi.hasNext()){
				Vector p = (Vector)pi.next();
				pa.curveVertex(p.x, p.y, p.z);
			}
			pa.endShape();
		}
	}

	public void keyAdded(Path p) {

		keysAdded(p, 1);
	}

	public void keysAdded(Path p, int amount) {
		for(int i = p.getSize() - amount; i < p.getSize(); i++){
			Vector v1, v2, v3;
			v2 = p.getKeyFrameByIndex(i).getPosition();
			v1 = v2;
			if(i > 0) v2 = p.getKeyFrameByIndex(i-1).getPosition();
			v3 = v2;
			if(i < p.getSize() - 1) v3 = p.getKeyFrameByIndex(i+1).getPosition();
			
			VectorSpace vs = VectorSpace.newVectorSpaceFromCurveAndCenter(v1, v2, v3, new Vector());
			
			Iterator li = lines.iterator();
			int k = 0;
			while(li.hasNext()){
				float a = (float)(k * 2 * Math.PI / lines.size());
				((ArrayList)li.next()).add(vs.getOrthoCoord(new Vector(0, width * (float)Math.sin(a), width * (float)Math.cos(a))));
				k++;
			}
		}
		
	}

	public void keyRemoved(Path p, int i) {
		Iterator li = lines.iterator();
		while(li.hasNext()){
			((ArrayList)li.next()).remove(i);
		}
	}

}
